import sys
import mechanize
from BeautifulSoup import *

def calculateUser(br, financing_product_id, proposal_id, grossMargin, escalator, initialPayment, instThisYear):
    url = "https://tools.cleanpowerfinance.com/quoting/financingcomparison/validate/on_success_action/analyze"
    data = "is_primary=1"
    data += "&margin=" + grossMargin
    data += "&proposal_id=" + proposal_id
    data += "&financing_product_id=" + financing_product_id
    data += "&id="
    data += "&install_this_year=" + instThisYear
    if initialPayment!="":
        data += "&down_payment=" + initialPayment
    if escalator!="":
        data += "&annual_increase=" + escalator
    if proposal_id==31:
        data += "&billing_surcharge=1"
    #print data
    response = br.open(url, data)
    html = response.read()
    soup = BeautifulSoup(html)
    table = soup.find("table")
    data = []
    trs = table.findAll('tr')
    for x in xrange(len(trs)):
        col = trs[x].findAll('td')
        colVal = col[0].findAll('b')
        if len(colVal)==0:
            data.append(col[0].string.replace("&nbsp;",""))
        else:
            data.append(colVal[0].string)
    return data
##
##br = mechanize.Browser(factory=mechanize.RobustFactory())
##br.set_handle_robots(False)
##br.open("https://tools.cleanpowerfinance.com/user/login")
##br.select_form("loginform")
##br["username"] = "lwooley@completesolar.com"
##br["password"] = "solar123solar"
##response = br.submit()
##
##calculateUser(br, '25', '50','173923', '2.9', '1000', '1')
